//
//  ExamController.m
//  作业-驾校
//
//  Created by qianfeng01 on 15-6-22.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "ExamController.h"
#import "FMDatabase.h"
#import "TestViewController.h"

#define path @"/Users/qianfeng01/xuyingjie/uI/Ui老师笔记/day20 sqlite数据库FMDB编程/homework/交规作业/data.sqlite"

@interface ExamController () <UITableViewDataSource,UITableViewDelegate>

{
    NSMutableArray *_dataArray;
    UITableView *_tableView;
    FMDatabase *_database;
}

@end

@implementation ExamController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBarHidden=NO;
    UIBarButtonItem *item=[[UIBarButtonItem alloc ] initWithTitle:@"主页" style:UIBarButtonItemStylePlain target:self action:@selector(Click:)];
    self.navigationItem.leftBarButtonItem=item;
    [self initModel];
    [self initUI];
}
-(void)Click:(id)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)initModel{
    _dataArray=[[NSMutableArray alloc] init];
    _database=[[FMDatabase alloc] initWithPath:path];
    if (![_database open]) {
        return;
    }
    NSString *sql=@"select * from firstlevel";
    FMResultSet *set=[_database executeQuery:sql];
    while ([set next]) {
        [_dataArray addObject:[set stringForColumn:@"pname"]];
    }
    [_database close];
}

-(void)initUI{
    _tableView=[[UITableView alloc ] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    [self.view addSubview:_tableView];
}

#pragma mark - <UITableViewDataSource,UITableViewDelegate>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID=@"cellID";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text=_dataArray[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TestViewController *controller=[[TestViewController alloc] init];
    controller.i=(int)indexPath.row+1;
    [self.navigationController pushViewController:controller animated:YES];
}

@end
